package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.zjitc.common.CustomException;
import net.zjitc.entity.Category;
import net.zjitc.entity.Dish;
import net.zjitc.entity.SetMeal;
import net.zjitc.mapper.CategoryMapper;
import net.zjitc.service.CategoryService;
import net.zjitc.service.DishService;
import net.zjitc.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl <CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetMealService setMealService;
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId,id);
        int count1=dishService.count(dishWrapper);
        if (count1>0){
            throw new CustomException("当前分类已关联菜品不能删除");
        }
        LambdaQueryWrapper<SetMeal> setMealWrapper = new LambdaQueryWrapper<>();
        setMealWrapper.eq(SetMeal::getCategoryId,id);
        int count2=setMealService.count(setMealWrapper);
        if (count2>0){
            throw new CustomException("当前分类已关联套餐不能删除");
        }
        super.removeById(id);
    }
}
